package com.zhoug.common.compat;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;

import java.lang.reflect.Field;

/**
 * NumberPicker 兼容工具
 *
 * @Author: zhoug
 * @Date: 2024-07-19
 * @Description:
 */
@SuppressLint("DiscouragedPrivateApi")
public class NumberPickerCompat {


    /**
     * 设置文字颜色
     *
     * @param numberPicker {@link  NumberPicker}
     * @param color        文字颜色
     */
    public static void setTextColor(NumberPicker numberPicker, int color) {
        if (numberPicker == null) return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            numberPicker.setTextColor(color);
        } else {
            int childCount = numberPicker.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = numberPicker.getChildAt(i);
                if (childAt instanceof EditText) {
                    ((EditText) childAt).setTextColor(color);
                }
            }
            try {
                Field mSelectorWheelPaintField = NumberPicker.class.getDeclaredField("mSelectorWheelPaint");
                mSelectorWheelPaintField.setAccessible(true);
                //设置分割线的颜色值
                Object mSelectorWheelPaint = mSelectorWheelPaintField.get(numberPicker);
                if (mSelectorWheelPaint instanceof Paint) {
                    ((Paint) mSelectorWheelPaint).setColor(color);
                }
                mSelectorWheelPaintField.setAccessible(false);
                numberPicker.invalidate();
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置文字大小
     * @param numberPicker
     * @param textSize
     */
    public static void setTextSize(NumberPicker numberPicker, float textSize) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            numberPicker.setTextSize(textSize);
        }else{
            int childCount = numberPicker.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = numberPicker.getChildAt(i);
                if (childAt instanceof EditText) {
                    ((EditText) childAt).setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
                }
            }
            try {
                Field mSelectorWheelPaintField = NumberPicker.class.getDeclaredField("mSelectorWheelPaint");
                mSelectorWheelPaintField.setAccessible(true);
                //设置分割线的颜色值
                Object mSelectorWheelPaint = mSelectorWheelPaintField.get(numberPicker);
                if (mSelectorWheelPaint instanceof Paint) {
                    ((Paint) mSelectorWheelPaint).setTextSize(textSize);
                }
                mSelectorWheelPaintField.setAccessible(false);
                numberPicker.invalidate();
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置分割线
     *
     * @param numberPicker {@link  NumberPicker}
     * @param dividerColor 分割线颜色
     */
    public static void setSelectionDivider(NumberPicker numberPicker, int dividerColor) {
        setSelectionDivider(numberPicker, new ColorDrawable(dividerColor));
    }

    /**
     * 设置分割线
     *
     * @param numberPicker {@link  NumberPicker}
     * @param divider 分割线
     */
    public static void setSelectionDivider(NumberPicker numberPicker, Drawable divider) {
        if (numberPicker == null) return;
        try {
            Field mSelectionDividerField = NumberPicker.class.getDeclaredField("mSelectionDivider");
            mSelectionDividerField.setAccessible(true);
            //设置分割线的颜色值
            mSelectionDividerField.set(numberPicker, divider);
            mSelectionDividerField.setAccessible(false);
            numberPicker.invalidate();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    /**
     * 利用反射设置分割线高度
     *
     * @param numberPicker {@link  NumberPicker}
     * @param height  分割线高度
     */
    public static void setSelectionDividerHeight(NumberPicker numberPicker, int height) {
        if (numberPicker == null) return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            numberPicker.setSelectionDividerHeight(height);
        } else {
            try {
                Field mSelectionDividerHeightField = NumberPicker.class.getDeclaredField("mSelectionDividerHeight");
                mSelectionDividerHeightField.setAccessible(true);
                //设置分割线的高度
                mSelectionDividerHeightField.set(numberPicker, height);
                mSelectionDividerHeightField.setAccessible(false);
                numberPicker.invalidate();
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }


}
